//
//  TCEquipmentViewController.m
//  Project_B
//
//  Created by dllo on 15/11/24.
//  Copyright © 2015年 张闯. All rights reserved.
//

#import "TCEquipmentViewController.h"
#import "AFNetworkHandler.h"
#import "MBProgressHUD.h"
#import "TCClothesViewController.h"
#import "TCSpecialTopicCollectionViewCell.h"

#import "UIImage+GIF.h"
//#import "TCMapEquipmentViewController.h"
#import "TCShopDetailViewController.h"
#import "TCShopMapTableViewCell.h"

#import "RESideMenu.h"


#import "RESideMenu.h"

#define WIDTH [[UIScreen mainScreen] bounds].size.width / 375
#define HEIGHT [[UIScreen mainScreen] bounds].size.height / 667
@interface TCEquipmentViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate>
//@property (nonatomic, retain)NSMutableArray *cateListArr;
//@property (nonatomic, retain)NSMutableArray *sports_catesArr;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)MBProgressHUD *HUD;
@property (nonatomic, strong)NSArray *arr;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, assign)NSInteger isold;
//@property (nonatomic, retain)NSArray *temp;
//@property (nonatomic, retain)NSDictionary *dic;
@property (nonatomic, retain)UICollectionView *collectionView;
@end

@implementation TCEquipmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
//    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;

//    self.navigationItem.title = @"装备分类";
    self.dataArr = [NSMutableArray array];
    self.arr = @[@"装备类型", @"运动方式",@"户外店"];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:60.0 / 255 green:149.0 / 255 blue:58.0 / 255 alpha:1];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-category"] style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController:)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]];
    
    [self getEquipment];
    [self creatNaviTitle];
}
- (void)creatCollection
{
    UICollectionViewFlowLayout *Layout = [[UICollectionViewFlowLayout alloc]init];
    Layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    Layout.itemSize = CGSizeMake(375*WIDTH, 667*HEIGHT);
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 375*WIDTH, 667*HEIGHT) collectionViewLayout:Layout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    Layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    Layout.minimumLineSpacing = 0;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[TCSpecialTopicCollectionViewCell class] forCellWithReuseIdentifier:@"myitem"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TCSpecialTopicCollectionViewCell *mycell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myitem" forIndexPath:indexPath];
    self.isold = indexPath.item;
    mycell.DataSource = self;
    mycell.Delegate = self;
    self.tableView = mycell.tableview;
    [self.tableView reloadData];
    return mycell;
}
- (void)getEquipment
{
    NSString *str = @"http://api.lis99.com/zhuangbei/fenlei/";
    [AFNetworkHandler requestPOSTWithURL:str body:nil comletion:^(id result) {
//        [self.cateListArr addObjectsFromArray:result[@"data"][@"cateList"]];
//        [self.sports_catesArr addObjectsFromArray:result[@"data"][@"sports_cates"]];
        if (result) {
            [self.dataArr addObject:result[@"data"][@"cateList"]];
            [self.dataArr addObject:result[@"data"][@"sports_cates"]];
        }
        else
        {
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"友情提示" message:@"检查是否连接网络" preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:^{

            }];
        }
        [AFNetworkHandler requestPOSTWithURL:@"http://api.lis99.com/shop/main/?latitude=38.88272599&longitude=121.53933059&offset=0&limit=20&cityid=1002&shoptype=&sorttype=&needcitys=1" body:nil comletion:^(id result) {
            if (result) {
                [self.dataArr addObject:result[@"data"][@"shop"]];
                //        NSLog(@"%@",result);
                [self creatCollection];
                [self.tableView reloadData];
            }
            else
            {
                UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                }];
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"友情提示" message:@"检查是否连接网络" preferredStyle:UIAlertControllerStyleAlert];
                [alert addAction:action];
                [self presentViewController:alert animated:YES completion:^{
                    
                }];
            }
           
            
        }];
        
        [self.HUD hide:YES];
    }];
}
- (void)creatNaviTitle
{
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.mode = MBProgressHUDModeIndeterminate;
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375*WIDTH, 40*HEIGHT)];
    self.navigationItem.titleView = titleView;
    
    for (NSInteger i = 0; i < self.arr.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake((375 / 4 * i+12)*WIDTH, 0, (375 / 3-40)*WIDTH, 40*HEIGHT);
        if (i == 0) {
            btn.titleLabel.font = [UIFont boldSystemFontOfSize:18];
            btn.titleLabel.alpha = 1;
        } else {
            btn.titleLabel.font = [UIFont systemFontOfSize:15];
            btn.titleLabel.alpha = 0.5;
        }
        [btn setTitle:self.arr[i] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 100 + i;
        [titleView addSubview:btn];
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    for (NSInteger i = 0; i < self.arr.count; i++) {
        if (self.collectionView.contentOffset.x / (375*WIDTH) == i) {
            UIButton *button = (UIButton *)[self.navigationItem.titleView viewWithTag:100 + i];
            button.titleLabel.font = [UIFont boldSystemFontOfSize:18];
            button.titleLabel.alpha = 1;
        }
        else {
            UIButton *button = (UIButton *)[self.navigationItem.titleView viewWithTag:100 + i];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            button.titleLabel.alpha = 0.5;
        }
    }
}
- (void)buttonAction:(UIButton *)btn
{
    [btn setShowsTouchWhenHighlighted:YES];
    for (NSInteger i = 0; i < self.arr.count; i++) {
        if (btn.tag == 100 + i) {
            btn.titleLabel.font = [UIFont boldSystemFontOfSize:18];
            btn.titleLabel.alpha = 1;
            self.collectionView.contentOffset = CGPointMake(375*WIDTH * i, 0);
        }
        if (btn.tag != 100 + i) {
            UIButton *button = (UIButton *)[self.navigationItem.titleView viewWithTag:100 + i];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            button.titleLabel.alpha = 0.5;
        }
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(self.isold == 2)
    {
        return 0;
    } else {
        return 40*HEIGHT;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 15)];
    view.backgroundColor = [UIColor colorWithRed:60.0 / 255 green:149.0 / 255 blue:58.0 / 255 alpha:0.3];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, self.view.frame.size.width, 15)];
    label.font = [UIFont systemFontOfSize:15];
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    
    if (self.isold == 0) {
        label.text = self.dataArr[self.isold][section][@"catname"];
    }
    else if(self.isold == 1)
    {
        label.text =  self.dataArr[self.isold][section][@"title"];
    }
    
    [view addSubview:label];
    return view;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.isold == 2) {
        return 1;
    }
    else{
    return (NSInteger)[self.dataArr[self.isold] count];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isold == 2) {
        return 130*HEIGHT;
    }
    return 50*HEIGHT;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
//    NSArray *temp = self.dataArr[section][@"list"];
    if (self.isold == 2) {
        return [self.dataArr[2] count];
    }
    else{
    return [self.dataArr[self.isold][section][@"list"] count];
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    TCShopMapTableViewCell *mapcell = [tableView dequeueReusableCellWithIdentifier:@"TCShopMapTableViewCell"];
//    NSArray *temp = self.dataArr[self.iso]
    if (self.isold == 0) {
        cell.textLabel.text = self.dataArr[self.isold][indexPath.section][@"list"][indexPath.row][@"catname"];
    }
    else if(self.isold == 1)
    {
        cell.textLabel.text = self.dataArr[self.isold][indexPath.section][@"list"][indexPath.row][@"title"];
    }
    else
    {
        mapcell.dic = self.dataArr[self.isold][indexPath.row];
//        cell.textLabel.text = self.dataArr[self.isold][indexPath.row][@"address"];
        return mapcell;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    TCClothesViewController *secVC = [[TCClothesViewController alloc]init];
    if (self.isold == 0) {
        secVC.detailID = [NSString stringWithFormat:@"http://api.lis99.com/zhuangbei/items/0/20/?cate_id=%@",self.dataArr[self.isold][indexPath.section][@"list"][indexPath.row][@"id"]];
        secVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:secVC animated:YES];
    }
    else if(self.isold == 1)
    {
        secVC.detailID = [NSString stringWithFormat:@"http://api.lis99.com/zhuangbei/items/0/20/?sport_id=%@",self.dataArr[self.isold][indexPath.section][@"list"][indexPath.row][@"id"]];
        secVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:secVC animated:YES];
    }
    else
    {
        TCShopDetailViewController *sec = [[TCShopDetailViewController alloc]init];
        sec.dic = self.dataArr[self.isold][indexPath.row];
        sec.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:sec animated:YES];
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
